package app

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"log"
)

var db *gorm.DB

type Database struct {
	User     string
	Password string
	Host     string
	Name     string
	Prefix   string
}

func GetDB() *gorm.DB {
	return db
}

func init() {
	conn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		"root",
		"",
		"127.0.0.1:3306",
		"game_activity",
	)

	var err error
	if db, err = gorm.Open("mysql", conn); err != nil {
		log.Fatalf("Fail to connect db : %v", err)
	}

	//defer db.Close()

	//gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
	//	return "" + defaultTableName
	//}
	//if Config.Env == "debug" {
	//	DB.LogMode(true)
	//}
	db.LogMode(true)

	//默认加s， grom转义struct名字的时候不用加上s
	db.SingularTable(true)

	// idle 空闲的
	db.DB().SetMaxIdleConns(10)
	db.DB().SetMaxOpenConns(20)
}
